module CatalogsHelper


  def search_catalog_filter_trees tree_name, page

    record_search_result = OpenStruct.new

    materials_facet = session[:catalog_filters_trees]
    #puts "@materials_facet: #{materials_facet}"

    if tree_name == :all_tree
      record_search_result.filter_tree_thematics      = sort_filter_tree materials_facet[:thematic_id], :thematics_filter, page
      record_search_result.filter_tree_authors        = sort_filter_tree materials_facet[:author_id], :authors_filter, page
      record_search_result.filter_tree_publishers     = sort_filter_tree materials_facet[:publisher_id], :editors_filter, page
      record_search_result.filter_tree_countries      = sort_filter_tree materials_facet[:publisher_country_id], :countries_filter, page
      record_search_result.filter_tree_years_editions = sort_filter_tree materials_facet[:edition_year], :years_filter, page
      record_search_result.filter_tree_editions       = sort_filter_tree materials_facet[:edition_number], :editions_filter, page

    else
      case tree_name
      when :thematics
        #materials_facets = Material.facets @search_engine_text, :facets => [:thematic_id], :with => @search_engine_with_options, :without  => @search_engine_without_options, :match_mode => @search_engine_match_mode, :group_clause   => "@count desc"
        record_search_result.filter_tree_thematics = sort_filter_tree materials_facet[:thematic_id], :thematics_filter, page
      when :authors
        #materials_facets = Material.facets @search_engine_text, :facets => [:author_id], :with => @search_engine_with_options, :without  => @search_engine_without_options, :match_mode => @search_engine_match_mode, :group_clause   => "@count desc"
        record_search_result.filter_tree_authors = sort_filter_tree materials_facet[:author_id], :authors_filter, page
      when :countries
        #materials_facets = Material.facets @search_engine_text, :facets => [:publisher_country_id], :with => @search_engine_with_options, :without  => @search_engine_without_options, :match_mode => @search_engine_match_mode, :group_clause   => "@count desc"
        record_search_result.filter_tree_countries = sort_filter_tree materials_facet[:publisher_country_id], :countries_filter, page
      when :publishers
        #materials_facets = Material.facets @search_engine_text, :facets => [:publisher_id], :with => @search_engine_with_options, :without  => @search_engine_without_options, :match_mode => @search_engine_match_mode, :group_clause   => "@count desc"
        record_search_result.filter_tree_publishers = sort_filter_tree materials_facet[:publisher_id], :editors_filter, page
      when :years_editions
        #materials_facets = Material.facets @search_engine_text, :facets => [:edition_year], :with => @@search_engine_with_options, :without  => @search_engine_without_options, :match_mode => @search_engine_match_mode, :group_clause   => "@count desc"
        record_search_result.filter_tree_years_editions = sort_filter_tree materials_facet[:edition_year], :years_filter, page
      when :editions
        #materials_facets = Material.facets @search_engine_text, :facets => [:edition_number], :with => @search_engine_with_options, :without  => @search_engine_without_options, :match_mode => @search_engine_match_mode, :group_clause   => "@count desc"
        record_search_result.filter_tree_editions = sort_filter_tree materials_facet[:edition_number], :editions_filter, page
      end
    end

    return record_search_result
  end

  def sort_filter_tree collection, filter, page
    #puts "colletion_hash = #{collection}" if filter == :authors_filter
    array = collection.to_a
    array = array.sort_by {|e| [-e[1], e[0]]}
    items_per_page = count_record_per_page filter
    return array.paginate :page => page, :per_page => items_per_page
  end

end
